package org.jupiter.blog.web.controller;

import jakarta.validation.Valid;
import org.jupiter.blog.api.dto.JupiterRoleDTO;
import org.jupiter.blog.api.dto.PageResponseDto;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/roles")
public class JupiterRoleController {

	@Autowired
	private JupiterRoleService roleService;

	/**
	 * 创建角色
	 */
	@PostMapping
	public JupiterResult<Void> createRole(@RequestBody @Valid JupiterRoleDTO dto) {
		roleService.createRole(dto);
		return JupiterResult.success(null);
	}

	/**
	 * 根据UUID获取角色
	 */
	@GetMapping("/{uuid}")
	public JupiterResult<JupiterRoleDTO> getRoleById(@PathVariable("uuid") String uuid) {
		return JupiterResult.success(roleService.getById(uuid));
	}

	/**
	 * 更新角色
	 */
	@PutMapping("/{uuid}")
	public JupiterResult<Void> updateRole(@PathVariable("uuid") String uuid, @RequestBody @Valid JupiterRoleDTO dto) {
		roleService.updateRole(uuid, dto);
		return JupiterResult.success(null);
	}

	/**
	 * 删除角色
	 */
	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> deleteRole(@PathVariable("uuid") String uuid) {
		roleService.deleteRole(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 分页查询角色列表
	 */
	@PostMapping("/page")
	public JupiterResult<PageResponseDto<JupiterRoleDTO>> getRolesByPage(@RequestBody JupiterRoleDTO queryDTO) {
		List<JupiterRoleDTO> roles = roleService.getByPage(queryDTO);
		long total = roleService.getCount(queryDTO);
		return JupiterResult.page(roles, total);
	}

	/**
	 * 获取所有角色
	 */
	@GetMapping("/getAllActiveRoles")
	public JupiterResult<List<JupiterRoleDTO>> getAllRoles() {
		return JupiterResult.success(roleService.getAll());
	}

	/**
	 * 根据用户ID获取角色列表
	 */
	@GetMapping("/user/{userId}")
	public JupiterResult<List<JupiterRoleDTO>> getRolesByUserId(@PathVariable("userId") String userId) {
		return JupiterResult.success(roleService.getByUserId(userId));
	}

	/**
	 * 恢复用户
	 */
	@RequestMapping(value = "/restoreRole/{uuid}", method = RequestMethod.POST)
	public JupiterResult<Void> restoreRole(@PathVariable("uuid") String uuid) {
		roleService.restoreRole(uuid);
		return JupiterResult.success(null);
	}

}
